package com.sixth.controller.wx;

import com.sixth.bean.MarketUser;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.common.BaseResultVo;
import com.sixth.bean.vo.WXSearchIndexVO;
import com.sixth.constant.Constant;
import com.sixth.service.WXSearchService;
import com.sixth.util.MarketInfoUtil;
import lombok.experimental.Helper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @since 2022/09/12 12:12
 * @author zhaoxiangyang
 */
@RestController
@RequestMapping("wx/search")
public class WXSearchController {

    @Autowired
    WXSearchService wxSearchService;

    @RequestMapping("index")
    public BaseRespVo wxSearchIndex(){
        // 获取登陆状态以及userId   暂时先假定一个userId=1；
        int id;
        MarketUser marketUser = MarketInfoUtil.getMarketUser();
        if(marketUser == null){
            id = -1;//标记未登录
        }else {
            id = marketUser.getId();
        }
        WXSearchIndexVO wxSearchIndexVO = wxSearchService.printIndex(id);
        return BaseRespVo.ok(wxSearchIndexVO);
    }


    // 清除搜索历史
    @PostMapping("clearhistory")
    public BaseResultVo wxSearchClearHistory() {
        // 只有登录用户才显示搜索历史，获取用户id，根据id删除搜索历史表中记录;
        MarketUser marketUser = MarketInfoUtil.getMarketUser();
        if (marketUser == null) {
            //如果没有未登录，不执行方法，直接返回0，成功
            return BaseResultVo.ok();
        }
        int userId = marketUser.getId();
        int code = wxSearchService.clearHistory(userId);
        if (code == 0) {
            return BaseResultVo.error("clear fail");
        }
        return BaseResultVo.ok();
    }

    @RequestMapping("helper")
    public BaseRespVo helper(String keyword){

       List<String> searchHistory = wxSearchService.helper(keyword);

       return BaseRespVo.ok(searchHistory);


    }

}
